//
//  ST_MatchedListForChatController.swift
//  btvideo
//
//  Created by mac on 2020/1/16.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import MJRefresh
import SwiftyJSON
import SwiftyUserDefaults

//消息匹配列表 -Controller
class ST_MatchedListForChatController: UIViewController{
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.white

         
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            ST_BTUserInfoManager.shared.refresh_my_sweetheart_list()
        })
        
        
//        collectionView.emptyDataSetSource = self
//        collectionView.emptyDataSetDelegate = self
        
//        view.addSubview(emptyView)
//        emptyView.snp.makeConstraints { (make) in
//            make.centerX.width.equalToSuperview()
//            make.height.equalTo(emptyView.figured_height)
//            make.centerY.equalToSuperview().offset( iPhoneXSeries ? -70 : -80)
//        }
//        self.request_data()
        
        NotificationCenter.default.addObserver(self, selector: #selector(refresh_data), name: Notification.Name.refresh_sweet_heart, object: nil)
        
    }
    
    
    func fetch_boot_data() {
        
        self.refresh_data()
    }
    
//    @objc func clear_data(){
//        currentPage = 0
//        self.data_array.removeAll()
//        self.collectionView.reloadData()
//    }
    
    @objc func refresh_data(){
        
        
        if let list = data_adapter.sweetheart_list,list.count > 0 {
            
            self.layout.headerReferenceSize = CGSize.zero
        }else{
            self.layout.headerReferenceSize = CGSize.init(width: ScreenWidth, height: ScreenHeight)
        }
        
        self.collectionView.reloadData {
            self.collectionView.mj_footer?.endRefreshing()
            self.collectionView.mj_header?.endRefreshing()
        }
    }
    
//    接着写
    @objc func like_ignore_hanlde(noti:Notification){
            
    //         ["accid": self.accid,"likeOption":true]
        /*
        let json = JSON.init(noti.userInfo ?? "")
        
        let accid = json["accid"].stringValue
        
        if json["likeOption"].boolValue == false{
            
            let item = self.data_array.removeFirst(where: {$0.accid == accid})
                
            if item != nil{
                
                self.collectionView.reloadData()
            }
        }
        */
        
    }

//    @objc fileprivate func request_data(){
//
//
//        self.collectionView.reloadData()
//    }

    //     MARK: - Properties


   fileprivate lazy var layout:UICollectionViewFlowLayout = {
      let controlLayout = UICollectionViewFlowLayout()
       controlLayout.minimumLineSpacing = 15
       controlLayout.minimumInteritemSpacing = 15
       controlLayout.sectionInset = UIEdgeInsets.init(top: 12, left: 15, bottom: BTSafeAreaBottom + 49, right: 15)
        let w = (ScreenWidth - 15*3)/2
        controlLayout.itemSize = CGSize.init(width: w, height: w/165*160+70)
       return controlLayout
   }()
   
   fileprivate lazy var collectionView:UICollectionView = {
       let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
       control.delegate = self
       control.dataSource = self
       control.register(cellWithClass: ST_MatchedListForChatCell.self)
    

    //        header
            control.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: MatchedListEmptyView.self)
       if #available(iOS 11.0, *) {
           control.contentInsetAdjustmentBehavior = .never
       }
       control.backgroundColor = Color.init(hexString: "#F5F8FA")
       control.alwaysBounceVertical = true
       control.showsVerticalScrollIndicator = false
       control.showsHorizontalScrollIndicator = false
       return control
   }()
    
    
    
    fileprivate var data_adapter = ST_BTUserInfoManager.shared

}

extension ST_MatchedListForChatController{
    
    @objc func title_click(){
        
    }
    @objc func right_click(){
        
    }
    @objc func left_click(){
        
        self.goback_previous_controller()
    }
}

extension ST_MatchedListForChatController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    // MARK: UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_adapter.sweetheart_list?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_MatchedListForChatCell.self, for: indexPath)
        
        cell.model = data_adapter.sweetheart_list?[safe:indexPath.item]
        
        return cell
    }

    // MARK: UICollectionViewDelegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let m = data_adapter.sweetheart_list?[safe:indexPath.item] else{
            return
        }

        let vc = ST_PairsCenterViewController()
        
        vc.accid = m.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withClass: MatchedListEmptyView.self, for: indexPath)
        
        return view
    }
    
}

//MARK: - Empty View
class MatchedListEmptyView: UICollectionReusableView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(emptyView)
        
        emptyView.snp.makeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.height.equalTo(emptyView.figured_height)
            make.centerY.equalToSuperview().offset( iPhoneXSeries ? -70 : -80)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var emptyView:ST_BTEmptyView = {
        
        let control = ST_BTEmptyView.init(title: "醒醒！你还没有配对。", imageName: ST_ImageUtils.imageName("消息_img_配对(空)")!, btnTitle: "添加特质") {
            
            push_to_push_peculiarity_controller(navigationController: self.parentViewController?.navigationController)
        }
        return control
    }()
}
